/*
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2025-06-12 14:59:08
 * @LastEditors: jqm123 2289662078@qq.com
 * @LastEditTime: 2025-07-23 09:52:06
 * @FilePath: /mobile_demo_v3/src/stores/user.store.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import api from "@/api/api";
import { getUserInfo, isZxcxAPP } from "@/utils/zxcxWebkit";
import { defineStore } from "pinia";

export const useUserStore = defineStore("user-store", {
  state: () => ({
    userData: JSON.parse(sessionStorage.getItem("userData")) || {},
    token: sessionStorage.getItem("token") || "",
  }),
  getters: {},
  actions: {
    //获取用户token
    checkToken() {},
    getUserInfo() {
      return new Promise(async (resolve, reject) => {
        let res = await getUserInfo();
        if (res) {
          resolve(JSON.parse(res));
        }
      });
    },
    async login() {
      let zxcxInfo = {};
      if (isZxcxAPP()) {
        zxcxInfo = await getUserInfo();
      } else if (import.meta.env.VITE_MODE === "development" && !isZxcxAPP()) {
        zxcxInfo = {
          openId: "00a2249a-a970-11ee-a160-286ed489a937",
        };
      }
      let res = await api.login({
        openId: zxcxInfo.openId,
      });
      this.token = res?.token;
      this.userData = res?.userInfo;
      sessionStorage.setItem("userData", JSON.stringify(res.userInfo));
      sessionStorage.setItem("token", res.token);
    },
  },
});
